package com.chagee.store.operation.server.service.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chagee.store.operation.api.response.StoreWorkOrderDeviceResponse;
import com.chagee.store.operation.api.response.SupplierConfirmationResponse;
import com.chagee.store.operation.api.response.WorkOrderDeviceResponse;
import com.chagee.store.operation.server.config.StoreOrderDeviceConfig;
import com.chagee.store.operation.server.entity.StoreOrderDeviceDO;
import com.chagee.store.operation.server.entity.WorkOrderDO;
import com.chagee.store.operation.server.event.service.WorkOrderBaseService;
import com.chagee.store.operation.server.service.base.WorkOrderDeviceBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName StoreWorkOrderEquipmentBizService  //类名称
 * @Description: 类描述
 * @Author: Jacky-zhan	//作者
 * @CreateDate: 2024/3/29 13:43	//创建时间
 * @UpdateUser: Jacky-zhan
 * @UpdateDate: 2024/3/29 13:43	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WorkOrderDeviceBizService {

	@Resource
	private WorkOrderDeviceBaseService workOrderDeviceBaseService;
	@Resource
	private WorkOrderBaseService workOrderBaseService;
	@Resource
	private StoreOrderDeviceConfig storeOrderDeviceConfig;

	/**
	 * 获取工单的交接的设备信息
	 *
	 * @param workOrderNumber 工单编号
	 * @return 交接设备对象
	 */
	public StoreWorkOrderDeviceResponse getDeviceList(String workOrderNumber) {
		StoreWorkOrderDeviceResponse storeWorkOrderDeviceResponse = new StoreWorkOrderDeviceResponse();
		//获取工单的特定信息
		LambdaQueryWrapper<WorkOrderDO> workOrderDoQueryWrapper = new LambdaQueryWrapper<>();
		workOrderDoQueryWrapper
			.eq(StringUtils.isNotBlank(workOrderNumber), WorkOrderDO::getWorkOrderNumber, workOrderNumber)
			.eq(WorkOrderDO::getDeleteFlag, NumberUtils.INTEGER_ZERO);
		WorkOrderDO workOrderDO = workOrderBaseService.getOne(workOrderDoQueryWrapper);
		if (Objects.nonNull(workOrderDO)) {
			// 工单表：1门店填写：前日清洁机会点、今日清洁重点及特殊待沟通事项，
			storeWorkOrderDeviceResponse.setWorkOrderId(workOrderDO.getId());
			storeWorkOrderDeviceResponse.setWorkOrderNumber(workOrderDO.getWorkOrderNumber());
			storeWorkOrderDeviceResponse.setTransfer(workOrderDO.getTransfer());
			// 工单表：2供应商确认信息(需要在创建工单的时候将2的json对象数据放进去),获取工单内的供应商确认结果（json数据-转成list返回）
			String result = workOrderDO.getDeviceTakeOver();
			List<SupplierConfirmationResponse> supplierConfirmationResponses = JSONArray.parseArray(result, SupplierConfirmationResponse.class);
			if (CollectionUtils.isNotEmpty(supplierConfirmationResponses)) {
				storeWorkOrderDeviceResponse.setDescVOList(supplierConfirmationResponses);
			}
			//获取门店工单对应的设备信息
			List<WorkOrderDeviceResponse> workOrderDeviceResponseList = new ArrayList<>();
			//添加：如果设备表没有找到关联关系，则去配置里取值，
			Map<String, String> map = JSONObject.parseObject(storeOrderDeviceConfig.getOrderDevice(), Map.class);
			if (!map.isEmpty()) {
				map.forEach((name, sign) -> {
					WorkOrderDeviceResponse workOrderDeviceResponse = new WorkOrderDeviceResponse();
					workOrderDeviceResponse.setWorkOrderNumber(workOrderDO.getWorkOrderNumber());
					workOrderDeviceResponse.setStoreCode(workOrderDO.getStoreCode());
					workOrderDeviceResponse.setDeviceName(name);
					workOrderDeviceResponse.setDeviceSign(sign);
					workOrderDeviceResponse.setDeviceNumber("-");
					workOrderDeviceResponse.setOperation(1);
					workOrderDeviceResponseList.add(workOrderDeviceResponse);
				});
			}
			if (CollectionUtils.isNotEmpty(workOrderDeviceResponseList)) {
				//获取工单关联的设备信息
				List<StoreOrderDeviceDO> storeOrderDeviceDOList = workOrderDeviceBaseService.getDeviceList(workOrderNumber);
				if (CollectionUtils.isNotEmpty(workOrderDeviceResponseList)) {
					workOrderDeviceResponseList.forEach(workOrderDeviceResponse -> {
						storeOrderDeviceDOList.stream()
							.filter(storeOrderDeviceDO -> storeOrderDeviceDO.getDeviceName().equals(workOrderDeviceResponse.getDeviceName()))
							.findFirst()
							.ifPresent(storeOrderDeviceDO -> {
								workOrderDeviceResponse.setDeviceNumber(String.valueOf(storeOrderDeviceDO.getDeviceNumber()));
								workOrderDeviceResponse.setOperation(storeOrderDeviceDO.getOperation());
								workOrderDeviceResponse.setRemark(storeOrderDeviceDO.getRemark());
							});
					});
				}
				storeWorkOrderDeviceResponse.setWorkOrderDeviceDetailVOList(workOrderDeviceResponseList);
			}
		}
		return storeWorkOrderDeviceResponse;
	}

}
